﻿using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.HSSF.Record;
using NPOI.XSSF.UserModel;

namespace WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase,IStudentBLL
    {
        public StudentController(IStudentBLL studentBLL)
        {
            StudentBLL = studentBLL;
        }

        public IStudentBLL StudentBLL { get; }
        [HttpPost]
        public Task<int> AddStudent(Student student)
        {
            return StudentBLL.AddStudent(student);
        }
        [HttpGet]
        public Student Login(string name, string pwd)
        {
            return StudentBLL.Login(name, pwd);
        }
        [HttpGet]
        public dynamic ShowStudent(int PageIndex, int PageSize)
        {
            return StudentBLL.ShowStudent(PageIndex, PageSize);
        }
        public IQueryable<Student> GetStudents()
        {
            return StudentBLL.GetStudents();
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Export()
        {
            var Stu = StudentBLL.GetStudents();
            XSSFWorkbook sheets = new XSSFWorkbook();
            var sheet= sheets.CreateSheet("学生信息表");
            var r= sheet.CreateRow(0);
            r.CreateCell(0).SetCellValue("编号");
            r.CreateCell(1).SetCellValue("姓名");
            r.CreateCell(2).SetCellValue("密码");
            r.CreateCell(3).SetCellValue("性别");
            r.CreateCell(4).SetCellValue("班级");
            r.CreateCell(5).SetCellValue("删除标识");
            r.CreateCell(6).SetCellValue("创建人编号");
            r.CreateCell(7).SetCellValue("创建时间");
            r.CreateCell(8).SetCellValue("删除人编号");
            r.CreateCell(9).SetCellValue("删除时间");
            r.CreateCell(10).SetCellValue("修改人编号");
            r.CreateCell(11).SetCellValue("修改时间");

            int num = 1;
            foreach (var item in Stu)
            {
                var R= sheet.CreateRow(num);
                R.CreateCell(0).SetCellValue(item.StudentId);
                R.CreateCell(1).SetCellValue(item.StudentName);
                R.CreateCell(2).SetCellValue(item.StudentPwd);
                R.CreateCell(3).SetCellValue(item.Sex);
                R.CreateCell(4).SetCellValue(item.StudentClass);
                R.CreateCell(5).SetCellValue(item.IsDel);
                R.CreateCell(6).SetCellValue(item.CreateUserId);
                R.CreateCell(7).SetCellValue(item.CreateTime);
                R.CreateCell(8).SetCellValue(item.DeleteUserId);
                R.CreateCell(9).SetCellValue(item.DeleteTime==null?"":item.DeleteTime.Value.ToString("yyyy-MM-dd hh:mm:ss"));
                R.CreateCell(10).SetCellValue(item.UpdateUserId);
                R.CreateCell(11).SetCellValue(item.UpdateTime==null?"": item.UpdateTime.Value.ToString("yyyy-MM-dd hh:mm:ss"));

                num++;
            }
            MemoryStream ms= new MemoryStream();
            sheets.Write(ms);
            return Ok();
        }

       
    }
}
